﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace OperateEdmx
{
    public partial class BatchModifyEdmProperties : Form
    {
        public BatchModifyEdmProperties()
        {
            InitializeComponent();
        }

        Dictionary<string, string> dict = new Dictionary<string, string>();

        private void button3_Click(object sender, EventArgs e)
        {
            Modify();
        }

        string url = "http://schemas.tempuri.com/CslaEntityExtension";
        string url1 = "http://schemas.tempuri.com/CslaPropertyExtension";
        string a1 = "a:CslaItemName";
        string a2 = "a:CslaCollectionName";
        string a3 = "a:CslaClassTemplate";
        string a4 = "a:CslaFriendlyName";

        private void Modify()
        {
            LoadDict();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(textBox1.Text);

            #region 
            foreach (System.Xml.XmlElement x in xmldoc.FirstChild.ChildNodes[1].ChildNodes[3].FirstChild.ChildNodes)
            {
                if (x.Name == "EntityType")
                {
                    string xname = x.Attributes["Name"].Value;
                    bool hasItemName = false;
                    bool hasCollectionName = false;
                    bool hasClassTemplate = false;

                    foreach (XmlElement xp in x)
                    {
                        string xpname = xp.Name;
                        if (xpname == a1)
                        {
                            hasItemName = true;
                            xp.InnerText = xname + (comboBox1.SelectedIndex == 0 ? "Edit" : "Info");
                        }
                        if (xpname == a2)
                        {
                            hasCollectionName = true;
                            xp.InnerText = xname + "List";
                        }
                        if (xpname == a3)
                        {
                            hasClassTemplate = true;
                            xp.InnerText = comboBox1.Text;
                        }
                        #region FriendName
                        if (xpname == "Property")
                        {
                            bool hasFrinendname = xp.HasChildNodes;
                            string pname = xp.Attributes["Name"].Value;
                            string key = xname.ToUpper() + "." + pname.ToUpper();
                            if (hasFrinendname)
                            {
                                if (dict.ContainsKey(key))
                                {
                                    xp.FirstChild.InnerText = dict[key];
                                }
                                else
                                {
                                    textBox3.Text += key + ",";
                                }
                            }
                            else
                            {
                                if (dict.ContainsKey(key))
                                {
                                    XmlNode nodefn = xmldoc.CreateNode(XmlNodeType.Element, a4, url1);
                                    nodefn.InnerText = dict[key];
                                    xp.AppendChild(nodefn);
                                }
                                else
                                {
                                    textBox3.Text += key + ",\r\n";
                                }
                            }
                        }
                        #endregion
                    }
                    #region Item ItemCollection Template
                    if (!hasItemName)
                    {
                        XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, a1, url);
                        node.InnerText = xname + (comboBox1.SelectedIndex == 0 ? "Edit" : "Info");
                        x.AppendChild(node);
                    }
                    if (!hasCollectionName)
                    {
                        XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, a2, url);
                        node.InnerText = xname + "List";
                        x.AppendChild(node);
                    }
                    if (!hasClassTemplate)
                    {
                        XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, a3, url);
                        node.InnerText = comboBox1.Text;
                        x.AppendChild(node);
                    }
                    #endregion
                }
            }
            #endregion
            xmldoc.Save(textBox1.Text);

        }

        private void LoadDict()
        {
            dict = new Dictionary<string, string>();
            FileInfo file = new FileInfo(textBox2.Text);

            using (FileStream fs = file.OpenRead())
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.Unicode))
                {
                    sr.BaseStream.Seek(0, SeekOrigin.Begin);
                    string str = sr.ReadLine();
                    while (str != null)
                    {
                        //当前行是空行 不添加数据
                        if (!str.Equals(string.Empty))
                        {
                            string[] ss = str.Split(",".ToArray(), StringSplitOptions.RemoveEmptyEntries);
                            dict.Add(ss[0], ss[1]);
                        }
                        str = sr.ReadLine();
                    }
                }
            }
        }
    }
}
